<?php
/**
 * Created by PhpStorm.
 * User: NickBai
 * Email: 876337011@qq.com
 * Date: 2019/2/17
 * Time: 11:33 AM
 */
namespace app\admin\controller;

use think\Db;
use app\admin\model\Admin;
use think\App;
use tool\Auth;

class Index extends Base
{
    public function index()
    {
        $authModel = new Auth();
        $menu = $authModel->getAuthMenu(session('admin_role_id'));

        $this->assign([
            'menu' => $menu
        ]);

        return $this->fetch();
    }

    public function home() {

        $total_today = Db::name('order')->whereTime('add_time','today')->where('status',1)->count();
        $total_yesterday = Db::name('order')->whereTime('add_time','yesterday')->where('status',1)->count();
        $total_week = Db::name('order')->whereTime('add_time','week')->where('status',1)->count();
        $total_lastweek = Db::name('order')->whereTime('add_time','last week')->where('status',1)->count();
        $total_month = Db::name('order')->whereTime('add_time','month')->where('status',1)->count();
        $total_lastmonth = Db::name('order')->whereTime('add_time','last month')->where('status',1)->count();
        $total_year = Db::name('order')->whereTime('add_time','year')->where('status',1)->count();
        $total_lastyear = Db::name('order')->whereTime('add_time','last year')->where('status',1)->count();

        $total_profit_month = Db::name('order_money')->whereTime('add_time','month')->sum('profit');
        $total_profit_lastmonth = Db::name('order_money')->whereTime('add_time','last month')->sum('profit');
        $total_profit_year = Db::name('order_money')->whereTime('add_time','year')->sum('profit');
        $total_profit_lastyear = Db::name('order_money')->whereTime('add_time','last year')->sum('profit');

        $total_pay_month = Db::name('order_money')->whereTime('add_time','month')->sum('pay');
        $total_pay_lastmonth = Db::name('order_money')->whereTime('add_time','last month')->sum('pay');
        $total_pay_year = Db::name('order_money')->whereTime('add_time','year')->sum('pay');
        $total_pay_lastyear = Db::name('order_money')->whereTime('add_time','last year')->sum('pay');
        // 总订单统计
        $totalOrder = Db::name('order')->where('status',1)->count();
        //已成交数据
        $totalOrderSuccess = Db::name('order')->where('state', 4)->where('status',1)->count();
        //总收入
        $totalProfit = Db::name('order_money')->sum('profit');
        //总支出
        $totalPay = Db::name('order_money')->sum('pay');


        $this->assign([
            'tp_version' => App::VERSION,
            'total_profit' => $totalProfit,
            'total_pay' => $totalPay,
            'total_order' => $totalOrder,
            'total_order_success' => $totalOrderSuccess,
            'total_today' => $total_today,
            'total_yesterday' => $total_yesterday,
            'total_week' => $total_week,
            'total_lastweek' => $total_lastweek,
            'total_month' => $total_month,
            'total_lastmonth' => $total_lastmonth,
            'total_year' => $total_year,
            'total_lastyear' => $total_lastyear,
            'total_profit_month' => $total_profit_month,
            'total_profit_lastmonth' => $total_profit_lastmonth,
            'total_profit_year' => $total_profit_year,
            'total_profit_lastyear' => $total_profit_lastyear,
            'total_pay_month' => $total_pay_month,
            'total_pay_lastmonth' => $total_pay_lastmonth,
            'total_pay_year' => $total_pay_year,
            'total_pay_lastyear' => $total_pay_lastyear,
        ]);
        return $this->fetch();
    }
    //最新10条预约信息
    public function tenOrder() {
        //最新订单
        $tenOrder = Db::name('order')->where('status',1)
            ->order('add_time', 'desc')->limit(10)->select();
        if($tenOrder) {
            return reMsg(0, $tenOrder, 'OK');
        } else {
            return reMsg(-1, '', '查询错误');
        }
    }

    // 修改密码
    public function editPwd()
    {
        if (request()->isPost()) {

            $param = input('post.');

            if ($param['new_password'] != $param['rep_password']) {
                return json(['code' => -1, 'data' => '', 'msg' => '两次密码输入不一致']);
            }

            // 检测旧密码
            $admin = new Admin();
            $adminInfo = $admin->getAdminInfo(session('admin_user_id'));

            if(0 != $adminInfo['code'] || empty($adminInfo['data'])){
                return json(['code' => -2, 'data' => '', 'msg' => '管理员不存在']);
            }

            if(!checkPassword($param['password'], $adminInfo['data']['admin_password'])){
                return json(['code' => -3, 'data' => '', 'msg' => '旧密码错误']);
            }

            $admin->updateAdminInfoById(session('admin_user_id'), [
                'admin_password' => makePassword($param['new_password'])
            ]);

            return json(['code' => 0, 'data' => '', 'msg' => '修改密码成功']);
        }

        return $this->fetch('pwd');
    }
}
